package com.anolesoft.yr.mbus.code.mapper;

import com.anolesoft.yr.mbus.code.entity.CodeMainDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.LocalDateTimeTypeHandler;

import java.util.List;

/**
 * @author zhj
 * @date 2020/9/22 9:44
 */
@Mapper
public interface CodeMainMapper {
    @Insert("insert into code_main (" +
            "objuid, " +
            "fac_code, " +
            "code_name, " +
            "code_desc, " +
            "geneclass," +
            "c_user_uid, " +
            "c_time, " +
            "m_user_uid, " +
            "m_time, " +
            "del_flag, " +
            "remark  " +
            ") values (" +
            "#{objuid,jdbcType=VARCHAR}, " +
            "#{factoryCode,jdbcType=VARCHAR}, " +
            "#{codeName,jdbcType=VARCHAR}, " +
            "#{codeDesc,jdbcType=VARCHAR}, " +
            "#{geneClass,jdbcType=VARCHAR}, " +
            "#{createUserUid,jdbcType=VARCHAR}, " +
            "now(), " +
            "#{modifyUserUid,jdbcType=VARCHAR}, " +
            "now(), " +
            "0," +
            "#{remark,jdbcType=VARCHAR} " +
            ")")
    int insert(CodeMainDO codeMainDO);

    @Update("update code_main set " +
            "fac_code = #{factoryCode,jdbcType=VARCHAR}," +
            "code_name = #{codeName,jdbcType=VARCHAR}, " +
            "code_desc = #{codeDesc,jdbcType=VARCHAR}, " +
            "geneclass = #{geneClass,jdbcType=VARCHAR}, " +
            "m_user_uid = #{modifyUserUid,jdbcType=VARCHAR}, " +
            "m_time = now(), " +
            "remark = #{remark,jdbcType=VARCHAR} " +
            " where " +
            "objuid = #{objuid,jdbcType=VARCHAR}")
    int update(CodeMainDO codeMainDO);

    @Update("update code_main set del_flag = 1 where objuid = #{id,jdbcType=VARCHAR}")
    int delete(@Param("id") String id);

    @Results(id = "CodeMainResultMap", value = {
            @Result(id = true, column = "objuid", property = "objuid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "fac_code", property = "factoryCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "code_name", property = "codeName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "code_desc", property = "codeDesc", jdbcType = JdbcType.VARCHAR),
            @Result(column = "geneclass", property = "geneClass", jdbcType = JdbcType.VARCHAR),

            @Result(column = "c_user_uid", property = "createUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "c_time", property = "createTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "m_user_uid", property = "modifyUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "m_time", property = "modifyTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "del_flag", property = "deleteFlag", jdbcType = JdbcType.BOOLEAN),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),

            @Result(column = "objuid", property = "codeItemRels", many=@Many(select = "com.anolesoft.yr.mbus.code.mapper.CodeItemRelMapper.selectListByCodeMainUid",fetchType = FetchType.LAZY)),
    })
    @Select("select " +
            "objuid, fac_code, code_name, code_desc, geneclass, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark  " +
            "from code_main where del_flag = 0 ")
    List<CodeMainDO> selectAll();

    @ResultMap("CodeMainResultMap")
    @Select("select " +
            "objuid, fac_code, code_name, code_desc, geneclass, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark  " +
            "from code_main where del_flag = 0 " +
            "and objuid = #{id,jdbcType=VARCHAR}")
    CodeMainDO selectOneById(@Param("id") String id);

    @ResultMap("CodeMainResultMap")
    @Select("select " +
            "objuid, fac_code, code_name, code_desc, geneclass, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark  " +
            "from code_main where del_flag = 0 " +
            "and code_name = #{codeName,jdbcType=VARCHAR}")
    CodeMainDO selectOneByCodeName(@Param("codeName") String codeName);

    @ResultMap("CodeMainResultMap")
    @Select("<script>" +
            "select " +
            "objuid, fac_code, code_name, code_desc, geneclass, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark  " +
            "from code_main where del_flag = 0 " +
            "<if test =\"o.factoryCode !=null and o.factoryCode != '' \">" +
            "   and fac_code = #{o.factoryCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.codeName !=null and o.codeName != '' \">" +
            "   and code_name like #{o.codeName, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.objuid !=null and o.objuid != '' \">" +
            "   and objuid = #{o.objuid, jdbcType=VARCHAR} " +
            "</if>" +
            "</script>")
    List<CodeMainDO> selectPageList(@Param("o") CodeMainDO codeMainDO, Page<CodeMainDO> page);

    @Select("select count(objuid) from code_main where del_flag = 0 and item_code_name = #{itemCodeName, jdbcType=VARCHAR}")
    Integer checkUniqueForItemCodeName(@Param("itemCodeName") String itemCodeName);

    @Select("select count(objuid) from code_main where del_flag = 0 and item_code_name = #{itemCodeName, jdbcType=VARCHAR} and objuid != #{id, jdbcType=VARCHAR}")
    Integer checkUniqueForItemCodeNameUpdate(@Param("itemCodeName") String itemCodeName, @Param("id") String id);
}
